import numpy as np
import pylab
import cv2

# 蓝色：值分布范围为[110,100,100]和[130,255,255]之间
# 绿色：值分布范围为[50,100,100]和[70,255,255]之间
# 红色：值分布范围为[0,100,100]和[10,255,255]之间
# 分离蓝色
img = cv2.imread("train_image/ball.png")  # 读取opencv图片

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)  # HSV空间

lower_blue = np.array([110, 100, 100])  # blue
upper_blue = np.array([130, 255, 255])

lower_green = np.array([60, 100, 100])  # green
upper_green = np.array([70, 255, 255])

lower_red = np.array([0, 100, 100])  # red
upper_red = np.array([10, 255, 255])

red_mask = cv2.inRange(hsv, lower_red, upper_red)  # 取红色
cv2.imshow('redMask', red_mask)
blue_mask = cv2.inRange(hsv, lower_blue, upper_blue)  # 蓝色
cv2.imshow('blue_mask', blue_mask)
green_mask = cv2.inRange(hsv, lower_green, upper_green)  # 绿色
cv2.imshow('green_mask', green_mask)


red = cv2.bitwise_and(img, img, mask=red_mask)  # 对原图像处理
green = cv2.bitwise_and(img, img, mask=green_mask)
blue = cv2.bitwise_and(img, img, mask=blue_mask)

res = green + red + blue

cv2.imshow('img', res)
cv2.waitKey(0)

cv2.destroyAllWindows()
